package com.bobo.rbac.auth.exception;


import com.alibaba.fastjson.JSON;
import com.bobo.rbac.auth.utils.ResponseResult;
import com.bobo.rbac.auth.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 认证失败异常
 * @author: BOBO
 * @time: 2022/7/23 15:42
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        Logger logger = LoggerFactory.getLogger(AuthenticationEntryPointImpl.class);
        ResponseResult result = ResponseResult.fail(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录");
        String json = JSON.toJSONString(result);
        logger.info("e--------------->"+e.getMessage());
        WebUtils.renderString(response,json);
    }
}
